Skip to content

[fix](nereids) Fix divide-by-zero guard in divideDecimal constant folding#64675

Open
LuciferYang wants to merge 2 commits into
apache:masterfrom
LuciferYang:fix/nereids-divide-decimal-zero-guard
Open

[fix](nereids) Fix divide-by-zero guard in divideDecimal constant folding#64675
LuciferYang wants to merge 2 commits into
apache:masterfrom
LuciferYang:fix/nereids-divide-decimal-zero-guard

Conversation

@LuciferYang

@LuciferYang LuciferYang commented Jun 22, 2026

Copy link
Copy Markdown

What problem does this PR solve?

Issue Number: close #64681

Problem Summary:

NumericArithmetic.divideDecimal (the DECIMALV2 constant-folding divide function) guarded against division by zero by checking the numerator (first) instead of the denominator (second):

public static Expression divideDecimal(DecimalLiteral first, DecimalLiteral second) {
    if (first.getValue().compareTo(BigDecimal.ZERO) == 0) {   // wrong operand
        return new NullLiteral(first.getDataType());
    }
    BigDecimal result = first.getValue().divide(second.getValue());
    return new DecimalLiteral(result);
}

As a result, when both operands are DECIMALV2 literals:

  • 0 / x folded to NULL instead of 0 — a silent wrong result.
  • x / 0 skipped the guard and called BigDecimal.divide(ZERO), throwing ArithmeticException. This is caught by ExpressionEvaluator.invoke and the expression is left unfolded (BE then evaluates it and returns NULL), so it is not a crash, but the FE folding path is incorrect.

The sibling functions divideDouble and divideDecimalV3 both correctly check second. This PR aligns divideDecimal with them by checking the denominator. Division by zero continues to return NULL, matching Doris/MySQL semantics (Doris is MySQL-compatible here; it does not raise the ANSI division by zero error).

Release note

Fix incorrect constant folding of DECIMALV2 division: 0 / x was folded to NULL instead of 0.

Check List (For Author)

  • Test

    • Unit Test (added testDivideDecimalZeroNumerator and testDivideDecimalZeroDenominator in NumericArithmeticTest)
  • Behavior changed:

    • Yes. 0 / x over constant DECIMALV2 values now correctly folds to 0 instead of NULL. Division by zero still returns NULL (unchanged).
  • Does this need documentation?

    • No.

…ding

divideDecimal checked the numerator (first) instead of the denominator (second) when guarding against division by zero. As a result DECIMALV2 constant folding evaluated 0 / x as NULL instead of 0, and x / 0 threw ArithmeticException (caught by ExpressionEvaluator and left unfolded). Guard on second, matching divideDouble and divideDecimalV3. Adds regression tests for both cases.
@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

…nt dispatch

Adds a FoldConstantRuleOnFE-level test that builds Divide over two DecimalV2 (DecimalLiteral) operands and verifies the full dispatch: 0 / 5 folds to decimal 0 (not NULL) and 5 / 0 folds to NULL. Complements the existing direct unit tests on the executable helper.
@LuciferYang LuciferYang marked this pull request as ready for review June 22, 2026 08:35

@yx-keith yx-keith left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@github-actions

Copy link
Copy Markdown
Contributor

PR approved by at least one committer and no changes requested.

@github-actions github-actions Bot added approved Indicates a PR has been approved by one committer. reviewed labels Jun 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

PR approved by anyone and no changes requested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by one committer. reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] DECIMALV2 constant folding: 0 / x folds to NULL instead of 0 (divideDecimal checks wrong operand)

3 participants